package com.zmm.util.password;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 900045
 * @description:
 * @name MdFiveUtil
 * @date By 2021-07-26 16:28:02
 */
public class MdFiveUtil {

	


	private MdFiveUtil() {
	}


	/**
	 * Returns a MessageDigest for the given <code>algorithm</code>.
	 *
	 *            The MessageDigest algorithm name.
	 * @return An MD5 digest instance.
	 * @throws RuntimeException
	 *             when a {@link NoSuchAlgorithmException} is
	 *             caught
	 */

	static MessageDigest getDigest() {
		try {
			return MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Calculates the MD5 digest and returns the value as a 16 element
	 * <code>byte[]</code>.
	 *
	 * @param data
	 *            Data to digest
	 * @return MD5 digest
	 */
	public static byte[] md5(byte[] data) {
		return getDigest().digest(data);
	}

	/**
	 * Calculates the MD5 digest and returns the value as a 16 element
	 * <code>byte[]</code>.
	 *
	 * @param data
	 *            Data to digest
	 * @return MD5 digest
	 */
	public static byte[] md5(String data, String charset) {
		if (charset == null) {
			return md5(data.getBytes());
		}
		try {
			return md5(data.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * Calculates the MD5 digest and returns the value as a 32 character hex
	 * string.
	 *
	 * @param data
	 *            Data to digest
	 * @return MD5 digest as a hex string
	 */
	public static String md5Hex(byte[] data) {
		return HexUtil.toHexString(md5(data));
	}

	/**
	 * Calculates the MD5 digest and returns the value as a 32 character hex
	 * string.
	 *
	 * @param data
	 *            Data to digest
	 * @return MD5 digest as a hex string
	 */
	public static String md5Hex(String data) {
		return HexUtil.toHexString(md5(data, null));
	}

	/**
	 * 指定编码加密
	 *
	 * @author lihe 2013-7-4 下午5:27:07
	 * @param data
	 * @param charset
	 * @return
	 * @see
	 * @since
	 */
	public static String md5Hex(String data, String charset) {
		return HexUtil.toHexString(md5(data, charset));
	}

	
	/**
	 * 解密登录密码
	 * @author: 900045
	 * @date: 2021-07-26 16:45:53
	 * @throws 
	 * @param password: 
	 * @return: java.lang.String
	 **/
	public static String getDecryptLoginPassword(String password) {
		return PasswordUtil.getDecryptLoginPassword(password);
	}

	public static String sign(String text, String key, String inputCharset) {
		String text1 = text;
		text1 = text1 + key;
		return md5Hex(text1, inputCharset);
	}
}
